<!--
 * @Descripttion: your project
 * @version: 1.0
 * @Author: hongweixun
 * @Date: 2022-06-28 18:06:09
 * @LastEditors: hongweixun
 * @LastEditTime: 2022-09-30 10:24:41
-->

<template>
  <div id="app">
    <router-view
      :key="route.fullPath"
      v-if="!route.meta.keetAlive"
    ></router-view>
    <router-view v-slot="{ Component }" :key="route.fullPath">
      <transition name="van-fade" mode="out-in">
        <keep-alive include="/index">
          <component :is="Component" v-if="route.meta.keetAlive" />
        </keep-alive>
      </transition>
    </router-view>
    <Footer @showOver="changeLay"></Footer>
    <van-overlay :show="overlayShow" @click="overlayShow = false" tem>
      <div class="wrapper">
        <div class="block">
          <van-image :src="gzhImg" fit="cover"></van-image>
        </div>
      </div>
    </van-overlay>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import * as dd from 'dingtalk-jsapi'
import {
  getQwLogin,
  getCode,
  getCodeByList
} from './components/Options/LoginQues'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import $util from './util/util'
import gzhImg from '@/assets/images/serve_img.png'
import { classDeclaration, objectTypeIndexer } from '@babel/types'
import { Toast } from 'vant'
const overlayShow = ref(false)
const route = useRoute()
const router = useRouter()
const store = useStore()
const code = ref('')
// 班级id。判断是不是扫二维码进来的
const classId = ref('')
const userType = ref('2')
const typeIndex = ref<any>(0)
const paramsList = ref<any>('')
const wechatQuery = ref({
  type: '',
  code: ''
})
const changeLay = () => {
  overlayShow.value = !overlayShow.value
}
// 获取学校钉钉cordId
const getCordId = async () => {
  const params = {
    type: 'get_CorpId',
    sign: 'hlzhjy'
  }
  const res: any = await getCode(params)
  if (res.code !== 1) {
    return Toast(res.msg)
  }
  // console.log(res.data)
  getDdCode(res.data)
}
// 网页加载即获取钉钉code信息
const getDdCode = (cordId: any) => {
  if (dd.env.platform !== 'notInDingTalk') {
    dd.ready(() => {
      // 使用SDK 获取免登授权码
      dd.runtime.permission.requestAuthCode({
        corpId: cordId,
        onSuccess: (info: any) => {
          code.value = info.code
          getList()
        },
        onFail: (err: any) => {
          // console.log('fail')
          console.log(JSON.stringify(err))
        }
      })
    })
  } else {
    console.log('不是钉钉服务器')
  }
}
// 根据钉钉code获取后台传过来的值
const getList = async () => {
  // type:1：学生，2：老师，3：家长
  const params = {
    code: code.value,
    type: userType.value
  }
  const res: any = await getCodeByList(params)
  if (res.code === 0) {
    return Toast(res.msg)
  }
  if (res.code === 999) {
    store.dispatch('setLoginForm', res.data)
    window.sessionStorage.setItem('accessToken', res.data.accessToken)
    window.sessionStorage.setItem('loginForm', JSON.stringify(res.data))
    if (classId.value !== '') {
      router.replace({
        path: '/index',
        query: {
          classId: classId.value
        }
      })
    } else {
      router.replace({
        path: '/homeView',
        query: {
          typeIndex: 9,
          params: paramsList.value
        }
      })
    }
  }
}
const getQYWechatList = async () => {
  // 获取企业微信的数值
  const params = {
    ...wechatQuery.value
  }
  const res: any = await getQwLogin(params)
  if (res.code === 0) {
    return Toast(res.msg)
  }
  if (res.code === 888) {
    // this.$toast(res.msg)
    // this.ddLoginForm = { ...this.loginForm, ...res.data }
    // this.showLoading = false
  }
  if (res.code === 999) {
    store.dispatch('setLoginForm', res.data)
    window.sessionStorage.setItem('accessToken', res.data.accessToken)
    window.sessionStorage.setItem('loginForm', JSON.stringify(res.data))
    if (Number(typeIndex.value) === 9) {
      router.replace({
        path: '/homeView',
        query: {
          typeIndex: 9,
          params: paramsList.value
        }
      })
    }
  }
}
onMounted(() => {
  // 禁止双指方法
  document.documentElement.addEventListener(
    'touchstart',
    function (event) {
      if (event.touches.length > 1) {
        event.preventDefault()
      }
    },
    false
  )
  // 禁止双击放大
  var lastTouchEnd = 0
  document.documentElement.addEventListener(
    'touchend',
    function (event) {
      var now = Date.now()
      if (now - lastTouchEnd <= 300) {
        event.preventDefault()
      }
      lastTouchEnd = now
    },
    false
  )
  const url = window.location.href
  // const url =
  //   'http://weixin.hlzhjy.com//Worker/?code=FBG02PYNbfVpMDuEpf4IdWwV4UNU8pFjG1RohwzyaGI&state=2and2and9and2022-08-29_6_第五节课and#/index'
  if (
    window.sessionStorage.getItem('accessToken') === null ||
    window.sessionStorage.getItem('accessToken') === undefined ||
    window.sessionStorage.getItem('accessToken') === ''
  ) {
    if (
      $util.GetArgsFromHref(url, 'code') !== '' &&
      $util.GetArgsFromHref(url, 'code') !== null
    ) {
      // 微信过来的
      wechatQuery.value.code = $util.GetArgsFromHref(url, 'code')
      if (
        $util.GetArgsFromHref(url, 'state').split('and') &&
        $util.GetArgsFromHref(url, 'state').split('and').length > 1
      ) {
        // 企业微信过来的
        wechatQuery.value.type = $util
          .GetArgsFromHref(url, 'state')
          .split('and')[0]
        // 发起请求
        if ($util.GetArgsFromHref(url, 'state').split('and')[2]) {
          typeIndex.value = $util.GetArgsFromHref(url, 'state').split('and')[2]
          paramsList.value = $util.GetArgsFromHref(url, 'state').split('and')[3]
          console.log($util.GetArgsFromHref(url, 'state').split('and')[2])
          getQYWechatList()
        }
      } else {
        wechatQuery.value.type = $util
          .GetArgsFromHref(url, 'state')
          .split('#')[0]
        // 发起请求
        // getWechatList()
      }
    } else {
      // 钉钉过来的
      // 发起请求
      if ($util.GetArgsFromHref(url, 'type')) {
        typeIndex.value = $util.GetArgsFromHref(url, 'type')
        // paramsList.value = $util.GetArgsFromHref(url, 'date')
        classId.value = $util.GetArgsFromHref(url, 'class_id')
      }
      // 网页加载即获取钉钉code信息
      getCordId()
    }
  }
})
</script>
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #333;
  position: relative;
  /* overflow: hidden; */
}
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.block {
  width: 100%;
  padding: 0 5%;
  height: auto;
  box-sizing: border-box;
  background-color: transparent;
}
.van-overlay {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 99 !important;
}
</style>
